import { Component } from '@angular/core';
import { Student } from './model/student';
import { StudentService } from './service/student.service';
import { Studentvo } from './vo/studentvo';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

//变量类型声明
textvalue:string;
numbervalue:number;

student:Student;
sl:Array<Student>;

studentvo:Studentvo;


constructor(private studentservice:StudentService){

}



//初始化方法
ngOnInit() {
  //变量初始化
  this.textvalue='今天天气真好！';
  this.numbervalue=58+70;

  this.studentvo=new Studentvo();
  this.sl=new Array();
  
  this.loadStudentList();

  this.student=new Student();
  // this.student.name="马荣志";
  // this.student.sex="男";  
  // this.student.stunum="201621447112";
  // this.student.birthday-new Date().getTime();
  // this.student.nativeplace="测试籍贯";

  // this.studentservice.getAnObject()
  // .then((data:any)=>{

  //   this.student=data;

  // })



}

//保存方法
add(){
  console.dir(this.studentvo);

  this.studentservice.addStudentInfo(this.studentvo).then((data:any)=>{
    this.studentservice.getAnObject()
    .then((data:any)=>{
      this.loadStudentList();
    })

  })


}
 

//载入student列表

loadStudentList(){

  this.studentservice.getStudentList()
  .then((data:any)=>{

    this.sl=data;

  })

}


//获得单个student对象

getToUpdateStudent(id:string){

  this.studentservice.getSingleStudent(id)
  .then((data:Student)=>{

    this.studentvo=new Studentvo();
    this.studentvo.studentid=data.studentid;
    this.studentvo.xh=data.stunum;
    this.studentvo.xm=data.name;


  })   

}


//删除student对象

deleteStudent(id:string){

  this.studentservice.deleteStudent(id)
  .then((flag:boolean)=>{

    if(flag==true){
      this.loadStudentList();
    }
    else{
      alert("操作异常，请刷新后重试");
    }



  })

}



}
